var calcEquation = function (equations, values, queries) {
  //构建有向图：{key为 变量，value为 {邻接变量：比值}}
  const graph = new Map()
  for (let i = 0; i < equations.length; i++) {
    const [a, b] = equations[i]
    const val = values[i]

    if (!graph.has(a)) graph.set(a, new Map())
    if (!graph.has(b)) graph.set(b, new Map())

    graph.get(a).set(b, val)
    graph.get(b).set(a, 1 / val)
  }

  //定义一个递归函数：在有向带权图中查找从起点start到终点end的路径，计算并返回路径上的权重乘积
  function dfs(start, end, visited) {
    //特殊情况处理
    if (!graph.has(start) || !graph.has(end)) return -1
    if (start === end) return 1

    //当前start已访问
    visited.add(start)
    //获取start的 {邻接变量：比值}
    const neighbors = graph.get(start)
    //遍历得到的neighbors
    for (const [neighbor, val] of neighbors.entries()) {
      //如果neighbor已访问，跳过
      if (visited.has(neighbor)) continue
      //递归搜索从neighbor到end的路径
      let result = dfs(neighbor, end, visited)
      //如果找到了有效路径，返回当前边权重乘以子路径的结果
      if (result !== -1) {
        return result * val
      }
    }

    //没有这个路径：返回-1
    return -1
  }

  //对数组queries使用map方法（调用dfs函数处理数组元素），最终返回数组queries
  return queries.map(([a, b]) => dfs(a, b, new Set()))
};